using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Xeml.Manager;
using Xeml.Sampling.Contracts;
using Xeml.Document;
using Mpi.Common;
using Xeml.Sampling;


namespace Xid.Controls
{
    public partial class SampleProviderWizard : UserControl
    {
        private int step = 0;
        private int provider = 0;
        private int providerCount = 0;
        //private ComponentInfoObject currentCompInf;
        private DataProviderResource currentProvRes;
        private IDataProvider currentProv;

        private int Step
        {
            get
            {
                return step;
            }
            set
            {
                this.step = value;
                if (step == 0)
                {
                    kryptonButtonStepForward.Values.Text = "Next";
                    kryptonButtonStepBack.Enabled = false;
                    groupBox1.Visible = true;
                    groupBoxConn.Visible = false;
                    kryptonButtonPrev.Visible = true;
                    kryptonButtonNext.Visible = true;


                    panel1.Controls.Clear();
                }
                if (step == 1)
                {
                    LoadComponent();
                    kryptonButtonStepForward.Values.Text = "Finish";
                    kryptonButtonStepBack.Enabled = true;
                    groupBox1.Visible = false;
                    groupBoxConn.Visible = true;
                    kryptonButtonPrev.Visible = false;
                    kryptonButtonNext.Visible = false;
                    
                    // currentProvRes.ComponentId = currentCompInf.Id;

                }
            }
        }

        private int Provider
        {
            get
            {
                return provider;
            }
            set
            {
                if (value >= 0 && value < providerCount)
                {
                    this.provider = value;

                    //currentCompInf = SpManager.Instance.ProviderComponents[provider - 1];
                    LoadCompInfo();
                    kryptonButtonPrev.Enabled = provider > 0;
                    kryptonButtonNext.Enabled = provider < providerCount-1;
                }
            }
        }



        private Dictionary<string, IDataProvider> _provider;
        private List<IDataProvider> _providerList;
        public SampleProviderWizard()
        {
            InitializeComponent();
            Step = 0;
            this.providerCount = SpManager.Instance.ProviderComponents.Count;
            _provider = SpManager.Instance.ProviderComponents;
            _providerList = new List<IDataProvider>(_provider.Values);
            currentProvRes = new DataProviderResource();
            Provider = 0;


        }

        private void LoadCompInfo()
        {
            if (currentProv != null)
                currentProv.AuthenticationRequest -= new EventHandler<Xeml.Sampling.AuthenticationRequestEventArgs>(currentProv_AuthenticationRequest);

            this.currentProv = SpManager.Instance.CreateProvider(_providerList[provider].Uri);
                currentProv.AuthenticationRequest += new EventHandler<Xeml.Sampling.AuthenticationRequestEventArgs>(currentProv_AuthenticationRequest);
            
            this.textBoxCompId.Text = currentProv.Uri;
            this.textBoxPublisher.Text = currentProv.Publisher;
            this.textBoxVersion.Text = currentProv.Version.ToString(3);
            this.textBoxCompName.Text = currentProv.ComponentName;
            this.textBoxDescription.Text = currentProv.Description;
        }

        private void LoadComponent()
        {

            //if (currentProv != null)
            //    currentProv.AuthenticationRequest -= new EventHandler<Xeml.Sampling.AuthenticationRequestEventArgs>(currentProv_AuthenticationRequest);

            //currentProv = SpManager.Instance.CreateProvider(currentCompInf.Id);
            //currentProv.AuthenticationRequest += new EventHandler<Xeml.Sampling.AuthenticationRequestEventArgs>(currentProv_AuthenticationRequest);


            //if (currentProv.SupportsConnectionStrBuild)
            //{
            //    foreach (CustomProperty<string> csp in currentProv.ConnectionStrProperties)
            //    {
            //        ComboBox lcc = new ComboBox();
            //       // lcc.Label = csp.Name;
                    
            //        lcc.Name = csp.Name;
            //        if (csp.PredefinedValues != null)
            //        {
            //            foreach (string v in csp.PredefinedValues)
            //            {
            //                lcc.Items.Add(v);
            //            }
            //        }
            //        lcc.DataBindings.Add("Value", csp, "Value", false, DataSourceUpdateMode.OnPropertyChanged);
            //        lcc.SelectedValue = csp.Default;
            //        if (csp.ForcePredefinedValues)
            //            lcc.DropDownStyle = ComboBoxStyle.DropDownList;
            //        else
            //            lcc.DropDownStyle = ComboBoxStyle.DropDown;

            //        //lcc.ToolTip = csp.Comment;
            //        lcc.Left = 5;
            //        lcc.Top = FindNextTopPixel();
            //        lcc.Width = panel1.Width - 10;
            //        panel1.Controls.Add(lcc);
            //    }


           // }



            LabelTextboxCtrl lc = new LabelTextboxCtrl();
            lc.Label = "ConnectionString";
            lc.DataBindings.Add("Value", currentProv, "ConnectionString", true, DataSourceUpdateMode.OnPropertyChanged);
            lc.DataBindings[0].ControlUpdateMode = ControlUpdateMode.OnPropertyChanged;
            //lc.ReadOnly = currentProv.SupportsConnectionStrBuild;
            lc.ToolTip = "The connection string for the provider.";
            lc.Left = 5;
            lc.Top = FindNextTopPixel() + 5;
            lc.Width = panel1.Width - 10;
            panel1.Controls.Add(lc);

            lc = new LabelTextboxCtrl();
            lc.Label = "Name";
            lc.DataBindings.Add("Value", currentProv, "FriendlyName", true, DataSourceUpdateMode.OnPropertyChanged);
            lc.DataBindings[0].ControlUpdateMode = ControlUpdateMode.OnPropertyChanged;

            lc.ToolTip = "A friendly name for the usage of this provider instance.";
            lc.Left = 5;
            lc.Top = FindNextTopPixel();
            lc.Width = panel1.Width - 10;
            panel1.Controls.Add(lc);


        }

        void currentProv_AuthenticationRequest(object sender, Xeml.Sampling.AuthenticationRequestEventArgs e)
        {
            AuthRequestForm arf = new AuthRequestForm();
            arf.AuthArgs = e;
            //arf.ProviderName = (sender as IDataProvider).;
            arf.ShowDialog();
        }

        private int FindNextTopPixel()
        {
            int ret = 5;
            foreach (Control c in panel1.Controls)
            {
                if(!(c is ComponentFactory.Krypton.Toolkit.KryptonButton))
                ret = Math.Max(c.Bottom + 2, ret);
            }
            return ret;
        }

        private void kryptonButtonPrev_Click(object sender, EventArgs e)
        {
            Provider--;
        }

        private void kryptonButtonNext_Click(object sender, EventArgs e)
        {
            Provider++;
        }

        private void kryptonButtonStepBack_Click(object sender, EventArgs e)
        {
            Step--;
        }

        private void kryptonButtonStepForward_Click(object sender, EventArgs e)
        {
            if (step == 1)
            {

                //currentProvRes.ComponentId = this.currentProv.ComponentId;
                //currentProvRes.ConnectionString = currentProv.ConnectionString;
                //currentProvRes.FriendlyName = currentProv.FriendlyName;
                //currentProvRes.Uri = this.currentProv.Uri;
                //currentProvRes.ConnectionString = currentProv.ConnectionString;
                //currentProvRes.FriendlyName = currentProv.FriendlyName;
                OnWizardFinished(currentProvRes);
                this.Visible = false;
            }
            else
                Step++;

        }

        public event EventHandler<SampleProviderWizardEventArgs> WizardFinished;
        protected virtual void OnWizardFinished(DataProviderResource res)
        {
            if (WizardFinished != null)
                WizardFinished(this, new SampleProviderWizardEventArgs(res));
        }

        private void kryptonButton1_Click(object sender, EventArgs e)
        {
          


            //ConnectResult cr = this.currentProv.TestConnection();
            //if (cr == ConnectResult.Established)
            //    MessageBox.Show("Connection test passed", "Connection test", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //else if (cr == ConnectResult.Failed)
            //    MessageBox.Show("Connection test failed", "Connection test", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void kryptonButtonCancel_Click(object sender, EventArgs e)
        {
            OnWizardFinished(null);
            this.Visible = false;
        }

      
    }

    public class SampleProviderWizardEventArgs : EventArgs
    {
        private DataProviderResource prov;

        public DataProviderResource Provider
        {
            get { return prov; }

        }

        public SampleProviderWizardEventArgs(DataProviderResource prov)
        {
            this.prov = prov;
        }

    }
}
